{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pandas 字符串编码与解码\n",
    "\n",
    "在数据处理中，我们经常会遇到不同编码格式的文本数据。尤其是在处理多语言文本时，正确地进行编码和解码至关重要。\n",
    "\n",
    "本教程将详细介绍如何在 Pandas 中使用 `.str.encode()` 和 `.str.decode()` 方法来处理字符串的编码和解码，并提供相关的代码示例。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 导入库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 准备数据\n",
    "\n",
    "我们创建一个包含多种语言字符的 Pandas Series，用于后续的编码解码操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = ['hello', 'world', '你好', '世界', '안녕하세요', 'こんにちは']\n",
    "s = pd.Series(data)\n",
    "print(\"原始数据:\")\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. 字符串编码 `.str.encode()`\n",
    "\n",
    "`.str.encode(encoding)` 方法可以将 Series 中的每个字符串从文本（str）类型转换为字节（bytes）类型。最常用的编码格式是 `utf-8`。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用 UTF-8 编码\n",
    "encoded_s = s.str.encode('utf-8')\n",
    "print(\"编码后的数据 (bytes):\")\n",
    "print(encoded_s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. 字符串解码 `.str.decode()`\n",
    "\n",
    "`.str.decode(encoding)` 方法则执行相反的操作，将字节（bytes）类型的数据转换回文本（str）类型。解码时必须使用与编码时相同的编码格式，否则会出错或产生乱码。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用 UTF-8 解码\n",
    "decoded_s = encoded_s.str.decode('utf-8')\n",
    "print(\"解码后的数据 (str):\")\n",
    "print(decoded_s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. 错误处理\n",
    "\n",
    "如果在解码时使用了错误的编码格式，Pandas 会抛出 `UnicodeDecodeError`。我们可以通过 `errors` 参数来控制错误处理的方式。\n",
    "\n",
    "- `errors='strict'` (默认): 遇到错误时抛出异常。\n",
    "- `errors='replace'`: 将无法解码的字节替换为特定标记 (通常是 ``)。\n",
    "- `errors='ignore'`: 直接忽略无法解码的字节。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 尝试使用错误的编码 (gbk) 解码 utf-8 数据，会引发错误\n",
    "try:\n",
    "    encoded_s.str.decode('gbk')\n",
    "except UnicodeDecodeError as e:\n",
    "    print(f\"解码出错: {e}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用 errors='replace' 处理错误\n",
    "decoded_replace = encoded_s.str.decode('gbk', errors='replace')\n",
    "print(\"使用 'replace' 处理错误:\")\n",
    "print(decoded_replace)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 使用 errors='ignore' 处理错误\n",
    "decoded_ignore = encoded_s.str.decode('gbk', errors='ignore')\n",
    "print(\"\\n使用 'ignore' 处理错误:\")\n",
    "print(decoded_ignore)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 6. 总结\n",
    "\n",
    "- **编码 (`.str.encode`)**: 字符串 -> 字节。用于存储或传输。\n",
    "- **解码 (`.str.decode`)**: 字节 -> 字符串。用于显示或处理。\n",
    "\n",
    "在进行编码和解码时，保持编码格式的一致性是避免数据损坏的关键。`utf-8` 是目前最通用、最推荐的编码格式。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
